//
// @author 风杨
// @date 22-9-14 上午10:38
// @see <a href="yueny09@163.com">mailTo: yueny09@163.com</a>
//
use config::{Config, Map, Value};

static mut CONFIG: Option<Config> = None;

// 初始化
pub fn init_config() {
    unsafe {
        if CONFIG.is_some() {
            return;
        }
    }
    let config = Config::builder()
        .add_source(config::File::with_name("config/base.yaml"))
        .build()
        .unwrap();
    unsafe {
        CONFIG = Some(config);
    }
}

// 获取配置。 对象为  &Config
pub fn get_config() -> &'static Config {
    unsafe {
        CONFIG.as_ref().expect("config not initialized!")
    }
}

#[test]
fn test_load_config() {
    init_config();

    let config = get_config();

    let storage_type = config.get_string("storage.type")
        .unwrap();
    assert!(storage_type.len() > 0);
    assert_eq!(storage_type, "s3");

    let storageS3Maps = config.get_table("storage.s3")
        .unwrap();
    assert!(storageS3Maps.len() > 0);
    assert_eq!(storageS3Maps.len(), 5);
    println!("storageS3Maps: {:#?}", storageS3Maps.clone());

    let _a = storageS3Maps.into_iter()
        .map(|(k, v)|
            (k, v.into_string().unwrap()));
    println!("{:#?}", _a);
    // assert_eq!(storageS3Maps.get("  endpoint"), "http://127.0.0.1:9000");
}
